package jwd.page

import android.databinding.DataBindingUtil
import android.databinding.ObservableArrayList
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.view.ViewPager
import jwd.adapter.ViewPagerAdapter
import jwd.base.BaseActivity
import jwd.model.corp.CorpFeture
import jwd.model.corp.CorpFetureGroup
import jwd.page.corp.CorpFragment
import jwd.page.me.MeFragment
import jwd.page.pzx.PzxFragment
import jwd.page.shop.ShopFragment
import jwd.pzx.R
import jwd.pzx.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import android.view.WindowManager
import android.annotation.TargetApi
import android.os.Build
import android.view.Window
import jwd.base.Config


class MainActivity : BaseActivity(R.layout.activity_main) {


    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_corp -> {
                viewPager.currentItem = 0
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_shop -> {
                //var view =  DataBindingUtil.inflate<ActivityMainBinding>(layoutInflater,pageId,null,false)
                viewPager.currentItem = 1
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_pzx -> {

                //setFragement(R.id.grid,BlankFragment("udi"))
                viewPager.currentItem = 2
                return@OnNavigationItemSelectedListener true
            }

            R.id.navigation_me -> {

                //setFragement(R.id.grid,BlankFragment("udi"))

                viewPager.currentItem = 3
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }



    lateinit var corpFunctionBinding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Config._Activity = this
        //setContentView(R.layout.activity_main)
        corpFunctionBinding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

        setupViewPager(viewPager)
    }

    private fun setupViewPager(viewPager: ViewPager) {
        val adapter = ViewPagerAdapter(supportFragmentManager)

        var dataCorp = ObservableArrayList<CorpFetureGroup>()
        dataCorp.add(CorpFetureGroup("常用功能", feature = mutableListOf(CorpFeture("录码", "/abc"))))

        adapter.addFragment(CorpFragment(dataCorp))
        adapter.addFragment(ShopFragment("商城"))
        adapter.addFragment(PzxFragment("品致信"))
        adapter.addFragment(MeFragment("我"))
        viewPager.offscreenPageLimit = adapter.count
        viewPager.adapter = adapter
        viewPager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener {
            override  fun onPageScrollStateChanged(state: Int) {

            }

            override  fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                navigation.menu.getItem(position).setChecked(true)
            }
        })
    }
}
